package com.javaweb.cwj.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.javaweb.cwj.entity.EduSubject;
import com.javaweb.cwj.entity.excel.SubjectData;
import com.javaweb.cwj.entity.subject.OneSubject;
import com.javaweb.cwj.entity.subject.TwoSubject;
import com.javaweb.cwj.listen.SubjectListen;
import com.javaweb.cwj.mapper.EduSubjectMapper;
import com.javaweb.cwj.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.val;
import org.apache.ibatis.annotations.One;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 璇剧▼绉戠洰 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-05-12
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Override
    public void saveSubject(MultipartFile file, EduSubjectService eduSubjectService) {
        try {
            //文件输入流
            InputStream in = file.getInputStream();
            //调用方法进行读取 默认读取第一个sheeet 会自动执行监听器的函数
            EasyExcel.read(in, SubjectData.class,new SubjectListen(eduSubjectService)).sheet().doRead();
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public List<OneSubject> findOneService() {
        //查询到一级标题的subject
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("parent_id", "0");
        List<EduSubject> oneSubjectList = baseMapper.selectList(queryWrapper);

        //查询到二级标题的subject
        QueryWrapper queryWrapper1 = new QueryWrapper();
        queryWrapper1.ne("parent_id", "0");
        List<EduSubject> twoSubjectList = baseMapper.selectList(queryWrapper1);
        //新建一个最终的对应前端的树形的数据
        List<OneSubject> oneFinalSubjects = new ArrayList<>();
        for (int i = 0; i < oneSubjectList.size(); i++) {
            EduSubject eduSubject = oneSubjectList.get(i);
            OneSubject oneSubject = new OneSubject();
            //BeanUtils.copyProperties(eduSubject, oneSubject);
            oneSubject.setId(eduSubject.getId());
            oneSubject.setTitle(eduSubject.getTitle());
            List<TwoSubject> twoFinaSubjects = new ArrayList<>();
            for (int m = 0; m < twoSubjectList.size(); m++) {
                //二级标题的subject
                EduSubject eduSubject1 = twoSubjectList.get(m);
                if (eduSubject.getId().equals(eduSubject1.getParentId())) {
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(eduSubject1, twoSubject);
                    twoFinaSubjects.add(twoSubject);
                }
            }
            oneSubject.setChildren(twoFinaSubjects);
            oneFinalSubjects.add(oneSubject);
        }

       return oneFinalSubjects;
    }


}
